package org.efficientsoap.test.client;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.efficientsoap.client.AsyncSOAPTask;
import org.efficientsoap.client.EfficientSoapDeserializer;
import org.efficientsoap.client.EfficientSoapHttpProxy;
import org.efficientsoap.client.EfficientSoapSerializer;
import org.efficientsoap.client.OnAsyncSOAPTaskCompleted;
import org.efficientsoap.test.client.R;
import org.efficientsoap.test.datatypes.ComplexData;
import org.efficientsoap.test.datatypes.PrimitiveData;
import org.efficientsoap.test.datatypes.RecursiveData;
import org.efficientsoap.test.datatypes.SimpleData;
import org.efficientsoap.test.deserializer.GenerateComplexDataResponseDeserializer;
import org.efficientsoap.test.results.GenerateComplexDataReturn;
import org.efficientsoap.test.serializer.GenerateComplexDataInputSerializer;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;


public class EfficientSoapActivity extends Activity implements OnAsyncSOAPTaskCompleted{

	private static final String TAG = EfficientSoapActivity.class
			.getSimpleName();
	XmlPullParser parser;
	private Button soapCallBtn;
	private ProgressBar progressBar;
	private EditText editText;
	private GenerateComplexDataReturn generateComplexDataReturn = new GenerateComplexDataReturn();
	private EfficientSoapDeserializer<?> eSoapDeserializer;
	private GenerateComplexDataInputSerializer bodySerializer;
	private AsyncSOAPTask soaptask;
	private EfficientSoapHttpProxy sender;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_efficient_soap);

		generateComplexDataReturn = new GenerateComplexDataReturn();
		try {

			eSoapDeserializer = new GenerateComplexDataResponseDeserializer();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}

		bodySerializer = new GenerateComplexDataInputSerializer();

		eSoapDeserializer.setSoapVersion(EfficientSoapDeserializer.VERSION11);
		bodySerializer.setSoapVersion(EfficientSoapSerializer.VERSION11);
		bodySerializer.addNamespaceToEnvelope("esns",
				"http://efficientsoap.org/test");
		bodySerializer.addNamespaceToEnvelope("esttype",
				"http://efficientsoap.org/test/datatypes");

		// eSoapParser.setBodyParser(getGlobalPropertiesResponseParser);// wenn
		// er es selber ist soll er es auch selber tun
		bodySerializer.setBodySerializer(bodySerializer);

		// GenerateComplexDataInput inputparam = new GenerateComplexDataInput();
		SimpleData inputparam = new SimpleData();

		PrimitiveData primitive1 = new PrimitiveData();
		primitive1.setIntegerValue(42);
		primitive1.setLongValue(123456789);
		primitive1.setDoubleValue(1.0);
		primitive1.setBooleanValue(true);
		PrimitiveData primitive2 = new PrimitiveData();
		primitive2.setLongValue(987654321);
		primitive2.setDoubleValue(1.570);
		primitive2.setBooleanValue(false);

		ArrayList<Integer> integerList = new ArrayList<Integer>();
		integerList.add(1);
		integerList.add(12);
		integerList.add(0123);

		SimpleData simple1 = new SimpleData();
		simple1.setStringValue(SimpleData.A_CONST);
		simple1.setIntegerList(integerList);
		simple1.setPrimitiveData(primitive2);

		SimpleData simple2 = new SimpleData();
		simple2.setStringValue("Simple2");
		integerList.add(2);
		simple2.setIntegerList(integerList);
		simple2.setPrimitiveData(primitive1);

		ArrayList<SimpleData> simpleList = new ArrayList<SimpleData>();
		simpleList.add(simple1);
		simpleList.add(simple2);

		ComplexData complex1 = new ComplexData();
		complex1.setPrimitiveData(primitive1);
		complex1.setSimpleData(simple1);
		complex1.setSimpleList(simpleList);

		ArrayList<ComplexData> complexList = new ArrayList<ComplexData>();
		complexList.add(complex1);
		complexList.add(null);
		complexList.add(complex1);

		RecursiveData recursive1 = new RecursiveData();
		recursive1.setComplexData(complex1);
		recursive1.setComplexList(complexList);
		recursive1.setRecursiveData(recursive1);
		recursive1.getRecursiveData().setRecursiveData(recursive1);

		inputparam.setStringValue("a SimpleData");
		inputparam.setPrimitiveData(primitive1);
		inputparam.setIntegerList(integerList);
		// bodySerializer.setObject2serialize(inputparam);

		bodySerializer.addParam("simpleParam", inputparam);

		// ProgressBar in Layout
		progressBar = new ProgressBar(this);
		progressBar = (ProgressBar) findViewById(R.id.progressBar1);
		progressBar.setProgress(0);
		progressBar.setMax(100);
		//progressBar.setVisibility(View.VISIBLE);
		
		sender = new EfficientSoapHttpProxy();
		sender.setHostname("192.168.1.16");
		sender.setPort(80);
		sender.setMethodpath("/test/testserver");
		sender.setEncoding("UTF-8");
		sender.setSocketTimeoutMs(500);
		

		
		soapCallBtn = (Button) findViewById(R.id.button1);

		soapCallBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				Activity act =(Activity) (view.getContext());
				soaptask = new AsyncSOAPTask( bodySerializer, eSoapDeserializer, sender , act, progressBar);
				soaptask.setIsvisibleOnlyOnProzess(true);
				//soaptask.setResetProgressBarOnPostExecute(true);
					        	

				String params = null;
				soaptask.execute(params);
			}
		});

		editText = (EditText) findViewById(R.id.editText1);
		
		if (null != eSoapDeserializer.getResponseObject()) {
			generateComplexDataReturn = (GenerateComplexDataReturn) eSoapDeserializer
					.getResponseObject();
			Log.d(TAG, generateComplexDataReturn.toString());
			editText.setText(generateComplexDataReturn.getComplexReturn().getPrimitiveData().getIntegerValue());
		}
	}
	@Override
	public void onAsyncSOAPTaskCompleted() {
		// TODO Auto-generated method stub
		
			try {
				eSoapDeserializer =  (EfficientSoapDeserializer<?>) soaptask.get();//(3,TimeUnit.MILLISECONDS);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			//eSoapDeserializer = soaptask.getEfficientSoapParser();
			generateComplexDataReturn = (GenerateComplexDataReturn) eSoapDeserializer.getResponseObject();
			if ( generateComplexDataReturn != null){
				Log.d(TAG, generateComplexDataReturn.toString());
				editText.setText(generateComplexDataReturn.getComplexReturn().getPrimitiveData().getIntegerValue());
			}
			Log.d(TAG, "completed");
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_efficient_soap, menu);
		return true;
	}



	
}
